package com.manage.generator.domain;

import com.manage.common.core.domain.BaseEntity;
import com.manage.common.utils.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 代码模板参数
 * @author: 喵师傅
 * @date: 2024/2/18 10:06
 */
public class GenParams extends BaseEntity {

    private String id;

    /**
     * 父节点id
     */
    private String parentId;

    /**
     * 属性名
     */
    private String propName;

    /**
     * 属性值类型
     */
    private String valType;

    /**
     * 属性值
     */
    private String val;

    /**
     * 备注
     */
    private String remark;

    /**
     * 顺序
     */
    private Integer sort;

    /**
     * 子节点
     */
    private List<GenParams> children;

    /**
     * 模板id
     */
    private Long templateId;

    /**
     * 参数集合转map
     * @param paramsList 参数集合
     * @return map
     */
    public static Map<String, Object> paramsListToMap(List<GenParams> paramsList) {
        Map<String, Object> paramsMap = new HashMap<>(16);
        if (!CollectionUtils.isEmpty(paramsList)) {
            for (GenParams params : paramsList) {
                if (params.getValType() == null) throw new RuntimeException("参数值类型不能为空");
                List<GenParams> childrenData = params.getChildren();
                Object val = null;
                switch (params.getValType()) {
                    case "string":
                        val = params.getVal();
                        break;
                    case "object":
                        if (!CollectionUtils.isEmpty(childrenData)) {
                            val = paramsListToMap(childrenData);
                        }
                        break;
                    case "array":
                        if (!CollectionUtils.isEmpty(childrenData)) {
                            List<Object> arrayList = new ArrayList<>();
                            for (GenParams childItem : childrenData) {
                                if ("string".equals(childItem.getValType())) {
                                    arrayList.add(childItem.getVal());
                                } else {
                                    arrayList.add(paramsListToMap(childItem.getChildren()));
                                }
                            }
                            val = arrayList;
                        }
                        break;
                    default:
                        throw new RuntimeException("参数值类型不正确");
                }
                paramsMap.put(params.getPropName(), val);
            }
        }
        return paramsMap;
    }

    /**
     * 参数集合转Tree
     * @param paramsList 参数集合
     * @return TreeList
     */
    public static List<GenParams> paramsListToTree(List<GenParams> paramsList) {
        List<GenParams> result = new ArrayList<>();

        if (!CollectionUtils.isEmpty(paramsList)) {
            Map<String, GenParams> nodeMap = new HashMap<>();
            // 首先将所有节点放入map中，以便快速查找
            for (GenParams node : paramsList) {
                nodeMap.put(node.getId(), node);
            }
            // 遍历所有节点，构建树结构
            for (GenParams node : paramsList) {
                if (StringUtils.isEmpty(node.getParentId())) {
                    // 如果没有父节点，说明是根节点
                    result.add(node);
                } else {
                    // 否则，找到父节点并将当前节点添加到父节点的子节点列表中
                    GenParams parent = nodeMap.get(node.getParentId());
                    if (parent != null) {
                        if (parent.getChildren() == null) {
                            parent.setChildren(new ArrayList<>());
                        }
                        parent.getChildren().add(node);
                    }
                }
            }
        }
        return result;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getPropName() {
        return propName;
    }

    public void setPropName(String propName) {
        this.propName = propName;
    }

    public String getValType() {
        return valType;
    }

    public void setValType(String valType) {
        this.valType = valType;
    }

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        this.val = val;
    }

    @Override
    public String getRemark() {
        return remark;
    }

    @Override
    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public List<GenParams> getChildren() {
        return children;
    }

    public void setChildren(List<GenParams> children) {
        this.children = children;
    }

    public Long getTemplateId() {
        return templateId;
    }

    public void setTemplateId(Long templateId) {
        this.templateId = templateId;
    }
}
